package com.yztc.videodemo;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;

/**
 * 演示录像功能
 */
public class MainActivity extends AppCompatActivity {
    private ImageView iv;
    private VideoView video;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv= (ImageView) findViewById(R.id.iv_show);
        video= (VideoView) findViewById(R.id.videoView);
    }
    /*
    点击录像按钮执行的操作
     */
    public void captureVideo(View view){
        Intent intent=new Intent();
        intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
        startActivityForResult(intent,1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1 && resultCode== Activity.RESULT_OK){
            //获取当前录制视频的uri路径
            Uri uri=data.getData();
            //启动contentResolver根据uri地址访问文件
            Cursor cursor=getContentResolver().query(uri,null,null,null,null);
            if(cursor.moveToFirst()){
                //当前视频的存放的路径
                String path=cursor.getString(cursor.getColumnIndex("_data"));

                //1.使用videoview播放录制的视频
                video.setVideoURI(Uri.parse(path));
//                video.setVideoPath(path);
                //添加videoview中视频控制器
                video.setMediaController(new MediaController(this));
                video.start();

                //2.使用ImageView展示视频的缩略图
                Bitmap bitmap= ThumbnailUtils.createVideoThumbnail(path,
                        MediaStore.Video.Thumbnails.MICRO_KIND);
                iv.setImageBitmap(bitmap);
            }
        }
    }
}













